package com.dx.weichat.weichat.wechatUtils;


import com.dx.weichat.weichat.entity.Msg;
import com.google.gson.Gson;
import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author: 丁鑫
 * @date: 2018/10/7/0007
 * @description: json2xml工具类
 */
public class JsonUtil {
	/**
	 * description:xml转json对象
	 * @param inputStream 传入流
	 * @return 返回json对象
	 * @throws JSONException
	 * @throws IOException
	 */
	public static JSONObject xml2jsonString(InputStream inputStream) throws JSONException, IOException {
		String xml = IOUtils.toString(inputStream);
		return XML.toJSONObject(xml);
	}

	/**
	 * description:json对象转xml格式
	 * @param jsonObject
	 * @return
	 */
	public static String jsonString2xml(JSONObject jsonObject){
		JSONObject result = new JSONObject();
		result.put("xml",jsonObject);
		return XML.toString(result);
	}
	/**消息对象转xml格式字符串*/
	public static String Obj2xmlByGson(Msg msg){
		JSONObject responseJson = new JSONObject(new Gson().toJson(msg));
		return JsonUtil.jsonString2xml(responseJson);
	}
}
